package com.jsoft.jsofted.sys.vo;

import com.diboot.core.util.ContextHelper;
import com.jsoft.jsofted.sys.entity.ActorEntity;
import com.jsoft.jsofted.sys.service.DeptService;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;

public class ActorTreeMaker {
    private List<ActorEntity> entityList;

    private LinkedHashMap<Long, ActorBean> nodes;

    private List<ActorBean> tree;

    private ActorTreeMaker(List<ActorEntity> list) {
        this.entityList = list;
    }

    public static List<ActorBean> make(List<ActorEntity> list) {
        ActorTreeMaker maker = new ActorTreeMaker(list);
        maker._makeNodes();
        maker._makeTree();
        return maker.tree;
    }

    public static List makeUserTree(List<ActorEntity> list
                                    // , DeptService deptService, PostService postService
    ) {
        ActorTreeMaker maker = new ActorTreeMaker(list);
        maker._makeNodes();
        maker._makeTree();
        maker._parsePostNodes();
        maker._parseDeptNodes();
        return maker.tree;
    }

    private void _makeNodes() {
        this.nodes = new LinkedHashMap();
        for (ActorEntity entity : entityList) {
            ActorBean node = new ActorBean();
            node.copyFrom(entity);
            this.nodes.put(entity.getId(), node);
        }
    }

    private void _makeTree() {
        this.tree = new LinkedList();
        for (ActorEntity entity : entityList) {
            ActorBean node = this.nodes.get(entity.getId());
            ActorBean parentNode = this.nodes.get(node.getParentId());

            if (parentNode == null) {
                this.tree.add(node);
            } else {
                parentNode.addChild(node);
            }
        }
    }

    /**
     * 解析职位节点
     */
    private void _parsePostNodes() {
        // TODO: 待完善
        DeptService deptService = (DeptService) ContextHelper.getBean("deptService");
//        PostService postService = (DeptService) ContextHelper.getBean("postService");
    }

    /**
     * 解析部门节点
     */
    private void _parseDeptNodes() {
        // TODO: 待完善
        DeptService deptService = (DeptService) ContextHelper.getBean("deptService");
    }

}
